﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>智能提示Demo</title>
    <script type="text/javascript">
    <!--
        var searchReq = createReq();
        //创建XMLHttpRequest的第二种方法
        function createReq() {
            var httpReq;

            if (window.XMLHttpRequest) {
                httpReq = new XMLHttpRequest();
                if (httpReq.overrideMimeType) {
                    httpReq.overrideMimeType('text/xml');
                }
            }
            else if (window.ActiveXObject) {
                try {
                    httpReq = new ActiveXObject('Msxml2.XMLHTTP');
                }
                catch (e) {
                    try {
                        httpReq = new ActiveXObject('Microsoft.XMLHTTP');
                    }
                    catch (e) {
                    }
                }
            }
            return httpReq;
        }
        //发送HTTP请求，当输入框的内容变化时，会调用该函数
        function searchSuggest() {
            var str = escape(document.getElementById("txtSearch").value);
            searchReq.open("get", "Server.aspx?searchText=" + str.trim(), true);
            searchReq.onreadystatechange = handleSearchSuggest;
            searchReq.send(null);
        }

        //当 onreadystatechange 值变化时，会调用该函数
        //注意searchSuggest()中的这一句searchReq.onreadystatechange = handleSearchSuggest;
        function handleSearchSuggest() {
            if (searchReq.readyState == 4) {
                if (searchReq.status == 200) {
                    var suggestText = document.getElementById("search_suggest");
                    var sourceText = searchReq.responseText.split("\n");
                    if (sourceText.length > 1) {
                        suggestText.style.display = "";
                        suggestText.innerHTML = "";
                        for (var i = 0; i < sourceText.length - 1; i++) {
                            var s = '<div onmouseover="javascript:suggestOver(this);"';
                            s += ' onmouseout="javascript:suggestOut(this);" ';
                            s += ' onclick="javascript:setSearch(this.innerHTML);" ';
                            s += ' class="suggest_link">' + sourceText[i] + '</div>';
                            suggestText.innerHTML += s;
                        }
                    }
                    else {
                        suggestText.style.display = "none";
                    }
                }
            }
        }

        function suggestOver(div_value) {
            div_value.className = "suggest_link_over";
        }

        function suggestOut(div_value) {
            div_value.className = "suggest_link";
        }

        function setSearch(obj) {
            document.getElementById("txtSearch").value = obj;
            var div = document.getElementById("search_suggest");
            div.innerHTML = "";
            div.style.display = "none";
        }

        function tbblur() {
            var div = document.getElementById("search_suggest");
            //div.innerHTML = "";
            div.style.display = "none";
        }
    //-->
    </script>
    <style type="text/css" media="screen">
        body
        {
            font: 14px arial;
        }
        .suggest_link
        {
            background-color: #FFFFFF;
            padding: 2px 6px 2px 6px;
            font-size: 15px;
        }
        .suggest_link_over
        {
            background-color: #E8F2FE;
            padding: 2px 6px 2px 6px;
            font-size: 15px;
        }
        #search_suggest
        {
            position: absolute;
            background-color: #FFFFFF;
            text-align: left;
            border: 1px solid #000000;
            width: 300px;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server" method="get">
    <div style="width: 250px;height:auto;">
        <input type="text" name="txtSearch" id="txtSearch" value="" onkeyup="searchSuggest();"
            runat="server" />
        <input type="submit" name="btnSearch" id="btnSearch" value="搜索" runat="server" />
        <br />
        <div id="search_suggest" style="display: none" runat="server">
        </div>
    </div>
    </form>
</body>
</html>
